﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

using BrightIdeasSoftware;
using Lib.Utils;

namespace MS.INV.MainData
{
    public partial class Catalogs : Lib.Bases.Forms.BaseForm
    {
        public Catalogs()
        {
            InitializeComponent();
        }

        private bool formLoad = false;
        private void Items_Load(object sender, EventArgs e)
        {
            try
            {
                formLoad = true;

                Reload();

                listView.OwnerDraw = true;
                listView.ItemRenderer = new BusinessCardRenderer();
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "Form load");
            }
            finally
            {
                formLoad = false;
            }
        }

        private void Reload()
        {
            DataTable DT = new DataTable();
            using (SysObj.L_HangHoa hh = new SysObj.L_HangHoa())
            {
                DT = hh.GetAll();
            }

            List<ItemObject> list = new List<ItemObject>();
            foreach (DataRow row in DT.Rows)
            {
                ItemObject item = new ItemObject();
                item.IdHangHoa = Cvert.ToInt32(row["IdHangHoa"], 0);
                item.MaHang = Cvert.ToString(row["MaHang"], "");
                item.TenHang = Cvert.ToString(row["TenHang"], "");
                item.TenDVT = Cvert.ToString(row["TenDVT"], "");
                item.TenNhom = Cvert.ToString(row["TenNhom"], "");
                item.TenLoai = Cvert.ToString(row["TenLoai"], "");
                item.HinhAnh = (byte[])row["HinhAnh"];
                list.Add(item);
            }
            listView.SetObjects(list);

            if (formLoad)
            {
                listView.ShowGroups = true;
                listView.AlwaysGroupByColumn = TenLoai;
                listView.BuildList();
            }
        }

        private void ShowHS(int ID)
        {
            Catalog_New ff = new Catalog_New(ID);
            ff.StartPosition = FormStartPosition.CenterScreen;
            ff.ShowDialog();
            if (ff.SaveChanged)
            {
                Reload();
            }
        }

        private void btTitle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            listView.View = View.Tile;
        }

        private void btList_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            listView.View = View.Details;
        }

        private void brRef_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Reload();
        }

        private void btNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ShowHS(0);
        }

        private void btClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void listView_DoubleClick(object sender, EventArgs e)
        {
            OLVListItem item = listView.SelectedItem;
            if (item == null) return;
            ItemObject obj = item.RowObject as ItemObject;
            if (obj == null) return;
            ShowHS(obj.IdHangHoa);
        }

    }
}
